namespace DotNetNuke.Security.Authentication
{
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Entities.Modules;
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Framework.Providers;
    using DotNetNuke.Security;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Runtime.CompilerServices;

    public class Configuration
    {
        private const string AUTHENTICATION_CONFIG_CACHE_PREFIX = "Authentication.Configuration";
        public const string AUTHENTICATION_KEY = "authentication";
        public const string AUTHENTICATION_LOGOFF_PAGE = "Logoff.aspx";
        public const string AUTHENTICATION_LOGON_PAGE = "WindowsSignin.aspx";
        public const string AUTHENTICATION_STATUS_KEY = "authentication.status";
        public const string LOGON_USER_VARIABLE = "LOGON_USER";
        private string mAuthenticationType = DefaultAuthenticationType;
        private string mEmailDomain = DefaultEmailDomain;
        private string mPassword = "";
        private int mPortalId;
        private string mProviderTypeName = DefaultProviderTypeName;
        private string mRootDomain = "";
        private bool mSynchronizePassword = false;
        private bool mSynchronizeRole = false;
        private string mUserName = "";
        private bool mWindowsAuthentication = false;

        /// -------------------------------------------------------------------
        /// <summary>
        /// - Obtain Authentication settings from database
        /// </summary>
        /// <remarks>
        /// - Setting records are stored in ModuleSettings table, separately for each portal,
        /// this method allows each portal could have different accessing method to Windows Active Directory
        /// </remarks>
        /// <history>
        /// [tamttt]	08/01/2004	Created
        /// </history>
        /// -------------------------------------------------------------------
        public Configuration()
        {
            PortalSettings _portalSettings = PortalController.GetCurrentPortalSettings();
            ProviderConfiguration _providerConfiguration = ProviderConfiguration.GetProviderConfiguration("authentication");
            this.mPortalId = _portalSettings.PortalId;
            PortalSecurity objSecurity = new PortalSecurity();
            try
            {
                if (_providerConfiguration.DefaultProvider != null)
                {
                    ModuleController objModules = new ModuleController();
                    Hashtable settings = PortalSettings.GetModuleSettings(objModules.GetModuleByDefinition(this.mPortalId, "Site Settings").ModuleID);
                    this.mWindowsAuthentication = Conversions.ToBoolean(Null.GetNull(RuntimeHelpers.GetObjectValue(settings["WindowsAuthentication"]), this.mWindowsAuthentication));
                    this.mSynchronizeRole = Conversions.ToBoolean(Null.GetNull(RuntimeHelpers.GetObjectValue(settings["SynchronizeRole"]), this.mSynchronizeRole));
                    this.mSynchronizePassword = Conversions.ToBoolean(Null.GetNull(RuntimeHelpers.GetObjectValue(settings["SynchronizePassword"]), this.mSynchronizePassword));
                    this.mRootDomain = Conversions.ToString(Null.GetNull(RuntimeHelpers.GetObjectValue(settings["RootDomain"]), this.mRootDomain));
                    this.mEmailDomain = Conversions.ToString(Null.GetNull(RuntimeHelpers.GetObjectValue(settings["EmailDomain"]), this.mEmailDomain));
                    this.mUserName = Conversions.ToString(Null.GetNull(RuntimeHelpers.GetObjectValue(settings["UserName"]), this.mUserName));
                    this.mProviderTypeName = Conversions.ToString(Null.GetNull(RuntimeHelpers.GetObjectValue(settings["ProviderTypeName"]), this.mProviderTypeName));
                    this.mAuthenticationType = Conversions.ToString(Null.GetNull(RuntimeHelpers.GetObjectValue(settings["AuthenticationType"]), this.mAuthenticationType));
                    this.mPassword = objSecurity.Decrypt("authentication", Conversions.ToString(Null.GetNull(RuntimeHelpers.GetObjectValue(settings["AuthenticationPassword"]), this.mPassword.ToString())));
                }
            }
            catch (Exception exception1)
            {
                
                Exception ex = exception1;
                
            }
        }

        /// -------------------------------------------------------------------
        /// <summary>
        /// Obtain Authentication Configuration
        /// </summary>
        /// <remarks>
        /// Accessing Active Directory also costs resource, 
        /// so we only do it once then save into cache for later use
        /// </remarks>
        /// <history>
        /// [tamttt]	08/01/2004	Created
        /// </history>
        /// -------------------------------------------------------------------
        public static Configuration GetConfig()
        {
            PortalSettings _portalSettings = PortalController.GetCurrentPortalSettings();
            string strKey = "Authentication.Configuration." + Conversions.ToString(_portalSettings.PortalId);
            if (DataCache.GetCache(strKey) == null)
            {
                Configuration config = new Configuration();
                DataCache.SetCache(strKey, config);
            }
            return (Configuration) DataCache.GetCache(strKey);
        }

        /// -------------------------------------------------------------------
        /// <summary>
        /// Used to determine if a valid input is provided, if not, return default value
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [tamttt]	08/01/2004	Created
        /// [tamttt]    08/20/2005  Replace by core Null.GetNull function
        /// </history>
        /// -------------------------------------------------------------------
        private string GetValue(object Input, string DefaultValue)
        {
            if (Input == null)
            {
                return DefaultValue;
            }
            return Conversions.ToString(Input);
        }

        /// -------------------------------------------------------------------
        /// <summary>
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [tamttt]	08/01/2004	Created
        /// </history>
        /// -------------------------------------------------------------------
        public static void ResetConfig()
        {
            PortalSettings _portalSettings = PortalController.GetCurrentPortalSettings();
            DataCache.RemoveCache("Authentication.Configuration." + Conversions.ToString(_portalSettings.PortalId));
        }

        /// -------------------------------------------------------------------
        /// <summary>
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [tamttt]	08/01/2004	Created
        /// </history>
        /// -------------------------------------------------------------------
        public static void UpdateConfig(int PortalID, bool WindowsAuthentication, string RootDomain, string EmailDomain, string AuthenticationUserName, string AuthenticationPassword, bool SynchronizeRole, bool SynchronizePassword, string ProviderTypeName, string AuthenticationType)
        {
            PortalSettings _portalSettings = PortalController.GetCurrentPortalSettings();
            ModuleController objModules = new ModuleController();
            PortalSecurity objSecurity = new PortalSecurity();
            int intModuleId = objModules.GetModuleByDefinition(PortalID, "Site Settings").ModuleID;
            objModules.UpdateModuleSetting(intModuleId, "WindowsAuthentication", WindowsAuthentication.ToString());
            objModules.UpdateModuleSetting(intModuleId, "SynchronizeRole", SynchronizeRole.ToString());
            objModules.UpdateModuleSetting(intModuleId, "SynchronizePassword", SynchronizePassword.ToString());
            objModules.UpdateModuleSetting(intModuleId, "RootDomain", RootDomain);
            objModules.UpdateModuleSetting(intModuleId, "EmailDomain", EmailDomain);
            objModules.UpdateModuleSetting(intModuleId, "UserName", AuthenticationUserName);
            objModules.UpdateModuleSetting(intModuleId, "ProviderTypeName", ProviderTypeName);
            objModules.UpdateModuleSetting(intModuleId, "AuthenticationType", AuthenticationType);
            if (AuthenticationPassword.Length > 0)
            {
                objModules.UpdateModuleSetting(intModuleId, "AuthenticationPassword", Convert.ToString(objSecurity.Encrypt("authentication", AuthenticationPassword)));
            }
        }

        /// -------------------------------------------------------------------
        /// <summary>
        /// </summary>
        /// <remarks>
        /// It was configured in web.config, move to site settings is more flexible
        /// When configure first time, only default provider (ADs) available to provide list of type to select 
        /// </remarks>
        /// <history>
        /// [tamttt]	08/01/2005	Created
        /// </history>
        /// -------------------------------------------------------------------
        public string AuthenticationType
        {
            get
            {
                return this.mAuthenticationType;
            }
        }

        /// -------------------------------------------------------------------
        /// <summary>
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [tamttt]	08/01/2004	Created
        /// </history>
        /// -------------------------------------------------------------------
        public static string DefaultAuthenticationType
        {
            get
            {
                return "Delegation";
            }
        }

        /// -------------------------------------------------------------------
        /// <summary>
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [tamttt]	08/01/2004	Created
        /// </history>
        /// -------------------------------------------------------------------
        public static string DefaultEmailDomain
        {
            get
            {
                string _portalEmail = PortalController.GetCurrentPortalSettings().Email;
                return _portalEmail.Substring(_portalEmail.IndexOf("@"));
            }
        }

        /// -------------------------------------------------------------------
        /// <summary>
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [tamttt]	08/01/2004	Created
        /// </history>
        /// -------------------------------------------------------------------
        public static string DefaultProviderTypeName
        {
            get
            {
                return "DotNetNuke.Security.Authentication.ADSIProvider, DotNetNuke.Authentication.ADSIProvider";
            }
        }

        /// -------------------------------------------------------------------
        /// <summary>
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [tamttt]	08/01/2004	Created
        /// </history>
        /// -------------------------------------------------------------------
        public string EmailDomain
        {
            get
            {
                return this.mEmailDomain;
            }
        }

        /// -------------------------------------------------------------------
        /// <summary>
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [tamttt]	08/01/2004	Created
        /// </history>
        /// -------------------------------------------------------------------
        public string Password
        {
            get
            {
                return this.mPassword;
            }
        }

        /// -------------------------------------------------------------------
        /// <summary>
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [tamttt]	08/01/2004	Created
        /// </history>
        /// -------------------------------------------------------------------
        public int PortalId
        {
            get
            {
                return this.mPortalId;
            }
        }

        /// -------------------------------------------------------------------
        /// <summary>
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [tamttt]	08/01/2004	Created
        /// </history>
        /// -------------------------------------------------------------------
        public string ProviderTypeName
        {
            get
            {
                return this.mProviderTypeName;
            }
        }

        /// -------------------------------------------------------------------
        /// <summary>
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [tamttt]	08/01/2004	Created
        /// </history>
        /// -------------------------------------------------------------------
        public string RootDomain
        {
            get
            {
                return this.mRootDomain;
            }
        }

        /// -------------------------------------------------------------------
        /// <summary>
        /// Process checking DNN password against Windows password
        /// update DNN password if not match
        /// requires modified signin page for functionality
        /// </summary>
        /// <remarks>
        /// This process quite expensive in terms of performance
        /// Reserve for future implementation
        /// </remarks>
        /// <history>
        /// [tamttt]	08/01/2005	Created
        /// </history>
        /// -------------------------------------------------------------------
        public bool SynchronizePassword
        {
            get
            {
                return this.mSynchronizePassword;
            }
        }

        /// -------------------------------------------------------------------
        /// <summary>
        /// Role membership to be synchronized (Authentication/DNN) when user logon
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [tamttt]	08/01/2004	Created
        /// </history>
        /// -------------------------------------------------------------------
        public bool SynchronizeRole
        {
            get
            {
                return this.mSynchronizeRole;
            }
        }

        /// -------------------------------------------------------------------
        /// <summary>
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [tamttt]	08/01/2004	Created
        /// </history>
        /// -------------------------------------------------------------------
        public string UserName
        {
            get
            {
                return this.mUserName;
            }
        }

        /// -------------------------------------------------------------------
        /// <summary>
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [tamttt]	08/01/2004	Created
        /// </history>
        /// -------------------------------------------------------------------
        public bool WindowsAuthentication
        {
            get
            {
                return this.mWindowsAuthentication;
            }
        }
    }
}

